/*
http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106

*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <cstdio>
using namespace std;

bool IsPopOrder(vector<int> pushV, vector<int> popV) {
  int i, j, n = pushV.size();
  stack<int> stk;
  if (n == 0) return 0;
  for (i = 0, j = 0; i < n;) {
    if (stk.empty() || stk.top() != popV[j]) {
      stk.push(pushV[i++]);
    } else {
      stk.pop();
      j++;
    }
  }
  for (; j < n && stk.top() == popV[j]; j++) stk.pop();

  return stk.empty();
}
int main() {
  //   int aa[]={1, 2, 3, 4, 5, 6},bb[]={6, 3,4, 1, 2, 5};
  int aa[] = {1, 2, 3, 4, 5, 6}, bb[] = {4, 5, 3, 2, 1, 6};
  vector<int> a(aa, aa + 6);
  vector<int> b(bb, bb + 6);
  cout << IsPopOrder(a, b);
  return 0;
}